package com.example.controller;

import com.example.entity.Equipment;
import com.example.entity.Venue;
import com.example.entity.VenueOrder;
import com.example.service.EquipmentService;
import com.example.service.VenueService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Api(tags = "场地管理模块",description = "场地的增、删、改、查、预定")
@Controller
@RequestMapping("/venue")
public class VenueController {
    @Resource
    VenueService venueService;

    @Resource
    EquipmentService equipmentService;

    @ApiOperation("查询全部场地")
    @GetMapping("/queryAllVenue")
    public String queryAllVenue(@RequestParam(value = "pageNum", required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                                Model model){
        PageInfo<Venue> venueList = venueService.queryAllVenue(pageNum,pageSize);
        model.addAttribute("venueList",venueList);
        return"Venue/venueList";
    }

    @ApiOperation("通过名字查询场地")
    @GetMapping("/queryVenueByName/{venueName}")
    public String queryVenueByName(@RequestParam(value = "pageNum", required = false,defaultValue = "1")Integer pageNum,
                                   @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                                   Model model,@PathVariable String venueName){
        PageInfo<Venue> venueList = venueService.queryVenueByName(pageNum,pageSize,venueName);
        model.addAttribute("venueList",venueList);
        return "Venue/venueList";
    }

    @ApiOperation("添加场地页面")
    @GetMapping("/toAddVenue{equipmentId}")
    public String toAddVenue(Model model,@PathVariable int equipmentId){
        List<Equipment> equipments = equipmentService.chargeEquipment(equipmentId);
        model.addAttribute("equipments",equipments);
        return "venue/addVenue";
    }

    @ApiOperation("保存场地")
    @PostMapping("/addVenue/{venue}")
    public int addVenue(@PathVariable Venue venue){
        return venueService.addVenue(venue);
    }

    @ApiOperation("删除场地")
    @GetMapping("/deleteVenue/{venueId}")
    public int deleteVenue(@PathVariable int venueId){
        return venueService.deleteVenue(venueId);
    }

    @ApiOperation("修改场地页面")
    @GetMapping("/toUpdateVenue/{venueId}")
    public String toUpdateVenue(@PathVariable int venueId, Model model){
        Venue venue = venueService.queryVenueById(venueId);
        model.addAttribute("venue",venue);
        return "venue/updateVenue";
    }

    @ApiOperation("修改场地")
    @PostMapping("/updateVenue/{venue}")
    public int updateVenue(@PathVariable Venue venue){
        return venueService.updateVenue(venue);
    }

    @ApiOperation("修改场地状态")
    @GetMapping("/updateVenueState/{venueId}{state}")
    public int updateVenueState(@PathVariable int venueId,@PathVariable int state){
        return venueService.updateVenueState(venueId,state);
    }

    @ApiOperation("查询场地费用")
    @GetMapping("/venueCharges/{venueId}")
    public String venueCharges(@PathVariable int venueId){
        return venueService.venueCharges(venueId);
    }

    @ApiOperation("查询场地一周信息")
    @GetMapping("/weeklyInfo/{venueId}")
    public String weeklyInfo(@PathVariable int venueId){
        return venueService.weeklyInfo(venueId);
    }

    @ApiOperation("场地预定页面")
    @GetMapping("/venueBook{venueId}")
    public String toVenueBook(@PathVariable int venueId, Model model){
        Venue venue = venueService.queryVenueById(venueId);
        model.addAttribute(venue);
        return "venue/venueBook";
    }

    @ApiOperation("保存订单信息")
    @PostMapping("/venueBook{startTime}{endTime}{venueId}")
    public int venueBook(@PathVariable String startTime,@PathVariable String endTime,@PathVariable int venueId) throws ParseException {
        return venueService.venueBook(startTime, endTime, venueId);
    }
}
